package pers.vinsonws.jianghu.serverspring.controller;

import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.vinsonws.jianghu.codeCompont.MsgEveryPro;
import pers.vinsonws.jianghu.serverspring.channel.ChannelManager;
import pers.vinsonws.jianghu.serverspring.entity.ApplyChatRooms;
import pers.vinsonws.jianghu.serverspring.entity.Users;
import pers.vinsonws.jianghu.serverspring.service.ApplyChatRoomServiceImpl;
import pers.vinsonws.jianghu.serverspring.service.ChatRoomServiceImpl;
import pers.vinsonws.jianghu.serverspring.service.UserServiceImpl;
import pers.vinsonws.jianghu.tools.DivisMessage;
import pers.vinsonws.jianghu.tools.MergeMessage;
import pers.vinsonws.jianghu.util.MessageLabel;

import java.util.Set;

/**
 * @auther vinsonws
 * @date 2018/11/6 21:54
 * 申请信息，当客户端申请加入聊天室，服务器只会记录下申请信息
 * 获取申请信息还有一种是获取一个用户的所有申请信息，并显示出通过与否
 * 获取申请信息，当管理员在客户端发送申请信息请求是将申请信息全部发送到管理员客户端
 * 操作：最后一个函数是关于操作申请的统一与否。
 */
@Component
public class ApplyChatRoomCtrl {
    private static final Logger log = LoggerFactory.getLogger(ApplyChatRoomCtrl.class);
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private ChatRoomServiceImpl chatRoomService;
    @Autowired
    private ApplyChatRoomServiceImpl applyChatRoomService;

    @Autowired
    private ChannelManager channelManager;

    public void recordApplyRrquest(MsgEveryPro.Message message){
        if (message.getType() != 3) throw new IllegalStateException("非法申请请求类型");
        ApplyChatRooms applyChatRooms = new ApplyChatRooms();
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        applyChatRooms.setChatRoomId(Integer.parseInt(divisMessage.getStrByIndex(1)));
        applyChatRooms.setUserId(message.getUserSendId());
        applyChatRooms.setStatus(false);
        applyChatRooms.setPass(false);
        applyChatRoomService.saveApplyRequest(applyChatRooms);
    }

    public Set<ApplyChatRooms> userGetApplyRecord(MsgEveryPro.Message message){
        if (message.getType() != 9) throw new IllegalStateException("非法用户查看申请请求类型");
        return applyChatRoomService.getApplyByUserId(message.getUserSendId());
    }

    public Set<ApplyChatRooms> adminGetApplyRecord(MsgEveryPro.Message message){
        if (message.getType() != 10) throw new IllegalStateException("非法管理员查看申请请求类型");
        return applyChatRoomService.getApplyByChatRoomId(message.getChatRoomOrUserRecvId());
    }

    public boolean operateApplyInfo(MsgEveryPro.Message message){
        if (message.getType() != 11) throw new IllegalStateException("非法管理员操作类型");
        DivisMessage divisMessage = new DivisMessage(message.getMessage(), message.getType());
        boolean b = applyChatRoomService.operateApply(message.getChatRoomOrUserRecvId(),
                        Integer.parseInt(divisMessage.getStrByIndex(2)),
                        divisMessage.getStrByIndex(1).equals("1"));
        //若同意将关系存入数据库
        if (divisMessage.getStrByIndex(1).equals("1")){
            userService.addChatRoomByUserId(Integer.parseInt(divisMessage.getStrByIndex(2)),
                    chatRoomService.getChatRoomsById(message.getChatRoomOrUserRecvId()));
        }

        if (b &&  divisMessage.getStrByIndex(1).equals("1")){
            Channel channel = channelManager.getChannelByUserId(Integer.parseInt(divisMessage.getStrByIndex(2)));
            if (channel != null){
                MsgEveryPro.Message.Builder messageSend = MsgEveryPro.Message.newBuilder();
                String messageStr;
                int belongToMe = 1;//属于
                int isMaster = 0;
                messageSend.setDateLong(System.currentTimeMillis());
                messageSend.setOnline(true);
                messageSend.setType(0);

                String[] messageArray = {String.valueOf(message.getChatRoomOrUserRecvId()),
                        chatRoomService.getChatRoomById(message.getChatRoomOrUserRecvId()).getName(),
                        String.valueOf(belongToMe), String.valueOf(isMaster)};
                messageStr = MergeMessage.infoToMessage(messageArray,0);
                messageSend.setMessage(messageStr);
                channel.writeAndFlush(messageSend.build());
                log.info("向用户发送通过通知成功");
            }


        }

        return b;
    }
}
